<?php
include "../database/database.php";

class MycouponBean
{
	public $couponid;
	public $couprice;
	public $id;
	public $coutitle;
	public $couimage;
	public $is_use;
	public $shop;
	public $shopid;
	public $isovertime;
	public $enddate;
	public $startdate;
	public $jianjie;
	public $top_price;
	public $type;
	public $fanwei;
	public $isReachTime;

	function __construct($couponId,$couPrice,$Id,$Title,$Images,$is_use,$shop,$shopid,$isovertime,$enddate,$startdate,$jianjie,$top_price,$type,$fanwei,$isReachTime)
	{
		$this->couponid = $couponId;
		$this->couprice = $couPrice;
		$this->id = $Id;
		$this->coutitle = $Title;
		$this->couimage = $Images;
		$this->is_use = $is_use;
		$this->shop = $shop;
		$this->shopid = $shopid;
		$this->isovertime = $isovertime;
		$this->enddate = $enddate;
		$this->startdate = $startdate;
		$this->jianjie = $jianjie;
		$this->top_price = $top_price;
		$this->type = $type;
		$this->fanwei = $fanwei;
		$this->isReachTime = $isReachTime;
	}
}

// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=10;
}
// 分页 end

$userid = $_GET['userid'];
$sql = "SELECT * FROM `mycoupon` WHERE `userid` = $userid  and is_delete=0  limit $de,$size";
$result = $conn->query($sql);

$MycouPon = array();
if ($result->fetch_assoc()['userid']==$userid) {
	while ($row = $result->fetch_assoc()) {
		$cou_id = $row['couponid'];
        //优惠券信息
		$coupon_info = $conn->query("SELECT * FROM `coupon` WHERE `couponid` = $cou_id ")->fetch_assoc();
        //获取该优惠券的商户名
		    $shop=0;
			$categroyids = $coupon_info[categroyids];
			$shopid = $coupon_info['shopid'];
			//获取该优惠券的商户名
			if($shopid!=0){ 
	            $sql2="select * from shop where shopid=".$shopid;
				$result2=$conn->query($sql2);
				$row2 = $result2->fetch_assoc();
				if($row2){
				   $shop = $row2[name];
				   $categroyids = $row2[leimu];
				 }
			}

		 $isovertime=0;
         //是否过期
         if($coupon_info['state']==2  ||  date('Y-m-d') > date($coupon_info[enddate]) ){
              $isovertime=1;
         }

         $isReachTime=1;
         //是否准许使用
         if(date('Y-m-d') < date($coupon_info[startdate])){
              $isReachTime=0;
         }

			//获取分类名称
			$cateNames = array();
			$categroyidsArray = explode(",", $categroyids);
			//平台优惠券
			if(in_array("0",$categroyidsArray)){
				$cateNames[] = '任意';
			}else{
				if($categroyids){
	                $sql2="select name from categroy where cateid in (".$categroyids.")";
					$result2=$conn->query($sql2);
					while ($row2 = $result2->fetch_assoc()) {
						$cateNames[] = $row2[name];
					}
				}
			}

		$MycouPon[] = new MycouponBean($coupon_info['couponid'],$coupon_info['price'],$row['id'],$coupon_info['title'],$coupon_info['srcimages'],$row['is_use'],$shop,$shopid,$isovertime,$coupon_info['enddate'],$coupon_info['startdate'],$coupon_info['jianjie'],$coupon_info['top_price'],$coupon_info['type'],$cateNames,$isReachTime);
		
	}
}

$conn->close();

echo json_encode(array('code' => 200,'msg' => "查询成功",'data' => $MycouPon ));
?>